/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tcpserver;

/**
 *
 * @author makkalot
 */
import java.io.*;
import java.net.*;

import udpserver.SaveImage;


public class SimpleTcpServer {

    private ServerSocket simpleserver = null;
    private SaveImage savei = null;
	
    public static final int bindPort = 11111; //evt hardcoded var mi?
    public static final int MAX_IO_BUFF = 4096;
	
    private BufferedInputStream server_bi = null;
    private DataInputStream server_di =null;

	
    public SimpleTcpServer() throws IOException{
		
	}
	
    public void runServer() throws  IOException{
        
        //bind the server please
        this.simpleserver = new ServerSocket(this.bindPort);
        
        System.out.println("The server is started on port :"+this.bindPort);
		//create a client socket
	while(true){
        
        this.savei = new SaveImage();  //open it to stay here for a wh
        Socket client_connection = null;
	client_connection = this.simpleserver.accept(); // it waits here some data comes
	
		//you can retrieve data with these
	this.server_bi = new BufferedInputStream(client_connection.getInputStream());
	this.server_di = new DataInputStream(this.server_bi);
		
		//we wont write data for now ... later maybe
	byte[] byte_seq = new byte[this.MAX_IO_BUFF]; //allocate for that
	
		
	while(true){
            
            //the data is stored into byte_array so we should copy it
            try{
                
                if ((this.server_di.read(byte_seq, 0, MAX_IO_BUFF))!=-1){
                    
                    System.out.println(byte_seq.toString());
                    
                    if(byte_seq.toString().equals("start")){
                        System.out.println("I got the start bit");
                    }
                    else if(byte_seq.toString().equals("stop"))
                        System.out.println("I got the stop bit");
                    
                   this.savei.writeThatByte(byte_seq,this.MAX_IO_BUFF); // save the byte array
                   System.out.println("I recieved :"+byte_seq.length+" bytes");
		
                }
                else{
                    System.out.println("We shouldnt got that ");
                    break;
                }
                        
                	
            }
            catch (IOException e) {
                System.err.println("Error in saving the retrieved data");
				
		}
            finally{
                this.server_di.close();
                this.server_bi.close();
                client_connection.close();
        
            }
        
	}
        // after that part close the file
            
		
        //after that part we shoul go up again
	System.out.println("Opening new file again");
        }
	}
	
	public static void main(String[] args){
		
	try{
            SimpleTcpServer simple_server = new SimpleTcpServer();
            simple_server.runServer();
        }
        catch(IOException e){
            System.err.println("Hop hop hata var");
            
        }
	}

}
